﻿/*
   RxInputManager
   Copyright 2011 Jordan Neumeyer

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   
   File: RxMouse.cs 
 */
using System;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using Microsoft.Xna.Framework.Input;

using RxInputManager.Extensions;
using RxInputManager.Interfaces;

namespace RxInputManager.Devices
{
    public class RxMouse : RxDevice<MouseState>, IRxMouse
    {
        public RxMouse()
            : this(RxInputScheduler.Scheduler)
        {
            DeviceName = "Mouse";
        }

        public RxMouse(IScheduler scheduler)
            : base(scheduler)
        {
            DeviceName = "Mouse";
        }

        public override bool IsAttached
        {
            get { return true; }
        }

        protected override MouseState CurrentState
        {
            get { return Mouse.GetState(); }
        }

        public IObservable<MouseState> MouseMove
        {
            get { return baseEvent.Where(PositionsAreNotEqual); }
        }

        public IObservable<MouseState> MouseDragAny()
        {
            return baseEvent.Where(PositionsAreNotEqual)
                            .Where(AnyHeld)
                            .TakeWhile(AnyHeld);
        }

        public IObservable<MouseState> MouseDrag(MouseButtons button)
        {
            return baseEvent.Where(PositionsAreNotEqual)
                            .Where(MouseStateHelper.GetSinglePressed(button))
                            .TakeWhile(MouseStateHelper.GetSinglePressed(button));
        }

        public IObservable<MouseState> MouseDrag(MouseButtons button, Action pressed, Action released)
        {
            bool pressedYet = false;
            return baseEvent.Where(MouseStateHelper.GetSinglePressed(button))
                            .Do(_ => { pressed(); pressedYet = true; })
                            .Take(1)
                            .Concat(baseEvent.Where(PositionsAreNotEqual)
                                             .TakeWhile(MouseStateHelper.GetSinglePressed(button)))
                            .TakeUntil(baseEvent.Where(s => pressedYet)
                                                .Where(MouseStateHelper.GetSingleReleased(button))
                                                .Take(1)
                                                .Do(_ => { released(); pressedYet = false; })
                                 )
                            .Repeat();
        }

        public IObservable<MouseState> MouseClick(MouseButtons button)
        {
            return baseEvent.Where(MouseStateHelper.GetSinglePressed(button))
                            .Where(_ => MouseStateHelper.GetSingleReleased(button)(previousState));
        }

        public IObservable<MouseState> MouseClick(MouseButtons button, Action pressed, Action released)
        {
            return baseEvent.Where(MouseStateHelper.GetSinglePressed(button))
                
                            .Do(_ => pressed())
                            .Where(_ => MouseStateHelper.GetSingleReleased(button)(previousState))
                            .Do(_ => released());
        }

        public IObservable<MouseState> MouseButtonHeld(MouseButtons button, int fireDelay = RxInputManager.Constants.DelayTime)
        {
            return baseEvent.Where(MouseStateHelper.GetSinglePressed(button))
                            .DelayEachUntil<MouseState>(baseEvent.SkipWhile(MouseStateHelper.GetSingleReleased(button)), TimeSpan.FromMilliseconds(fireDelay));            
        }

        // Not the best way, but well work.
        private bool PositionsAreEqual(MouseState state)
        {
            return state.X - previousState.X == 0 && state.Y - previousState.Y == 0;
        }

        private bool PositionsAreNotEqual(MouseState state)
        {
            return state.X - previousState.X != 0 || state.Y - previousState.Y != 0;
        }

        private bool AnyHeld(MouseState state)
        {
            return state.LeftButton == ButtonState.Pressed ||
                   state.RightButton == ButtonState.Pressed ||
                   state.MiddleButton == ButtonState.Pressed ||
                   state.XButton1 == ButtonState.Pressed ||
                   state.XButton2 == ButtonState.Pressed;
        }


        public IObservable<MouseState> MouseClickAny()
        {
            return baseEvent.Where(AnyHeld)
                            .Where(_ => !AnyHeld(previousState));
        }

        public IObservable<int> MouseWheel(int threshold = 0)
        {
            if(threshold > 0)
                return baseEvent.Where(state => state.ScrollWheelValue > threshold)
                                .Select(state => state.ScrollWheelValue)
                                .DistinctUntilChanged();
            else
                return baseEvent.Select(state => state.ScrollWheelValue)
                                .DistinctUntilChanged();
        }
    }
}
